package com.mm.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.mapper.GeneratorMapper;
import com.mm.entity.vo.ColumnInfo;
import com.mm.entity.vo.TableInfo;
import com.mm.utils.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Zheng Jie
 * @date 2019-01-02
 */
@RestController
@RequestMapping("/generator")
@Api(tags = "代码生成管理")
public class GeneratorController {

    @Autowired
    GeneratorMapper generatorMapper;

//    @Value("${generator.enabled}")
//    private Boolean generatorEnabled;


    @ApiOperation("查询数据库表")
    @GetMapping(value = "/tables")
    public ApiResult<TableInfo> queryTables(@RequestParam(defaultValue = "1") Integer pageNo,
                                            @RequestParam(defaultValue = "10") Integer pageSize) {
        return ApiResult.success(generatorMapper.getTables(new Page(pageNo,pageSize)));
    }

    @ApiOperation("查询字段数据")
    @GetMapping(value = "/columns")
    public ApiResult<ColumnInfo> queryColumns(@RequestParam String tableName) {
        List<ColumnInfo> columnInfos = generatorMapper.getTableColumns(tableName);
        return ApiResult.success(columnInfos);
    }

  /*  @ApiOperation("保存字段数据")
    @PutMapping
    public ResponseEntity<HttpStatus> save(@RequestBody List<ColumnInfo> columnInfos) {
        generatorService.save(columnInfos);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("同步字段数据")
    @PostMapping(value = "sync")
    public ResponseEntity<HttpStatus> sync(@RequestBody List<String> tables) {
        for (String table : tables) {
            generatorService.sync(generatorService.getColumns(table), generatorService.query(table));
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("生成代码")
    @PostMapping(value = "/{tableName}/{type}")
    public ResponseEntity<Object> generator(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response) {
        if (!generatorEnabled && type == 0) {
            throw new BadRequestException("此环境不允许生成代码，请选择预览或者下载查看！");
        }
        switch (type) {
            // 生成代码
            case 0:
                generatorService.generator(genConfigService.find(tableName), generatorService.getColumns(tableName));
                break;
            // 预览
            case 1:
                return generatorService.preview(genConfigService.find(tableName), generatorService.getColumns(tableName));
            // 打包
            case 2:
                generatorService.download(genConfigService.find(tableName), generatorService.getColumns(tableName), request, response);
                break;
            default:
                throw new BadRequestException("没有这个选项");
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }*/
}
